package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.util.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Client: RCS.
 * Action: Removes a Group from the Database. 
 * The Source and Configs enclosed within the Observations are also patched in.
 * @author S.N.Fraser
 */

public class REMOVE_GROUPImpl extends TransactionImpl {
    
    /** The path used to locate the Group in the OSS database. */
    protected Path groupPath;

    /** */ 
    public REMOVE_GROUPImpl(REMOVE_GROUP request) {
	super(request);
	this.groupPath = request.getGroupPath();	
    }
    
    /**
     * Add the Group.
     * @return Boolean description of operation or an OSS_Error.
     */
    public TRANSACTION_DONE exec() {
	
	Logger logger = LogManager.getLogger(this);
	
	Transaction t = null;
	try {
	    t = Transaction.current();

	    // Fetch the Proposal.
	    Proposal proposal = null;
	    
	    try {
		proposal = OSS_Utilities.fetchProposal(groupPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+groupPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
			     "Proposal: "+groupPath.toString()+
			     " Error reading from database",
			     iox);
	    }		
	    
	    // If its not there fail.
	    if (proposal == null) {
		logger.log(1, "REMOVE_GROUP_Impl", id, "exec",
			   "Proposal: "+groupPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Proposal: "+groupPath.toString());
	    }
	    
	    // Check if Proposal locked.
	    if (proposal.isLocked()) {
		//#### maybe this should be an ILLEGAL_DB_ACCESS ?
		return error(TRANSACTION.ILLEGAL_DATABASE_ACCESS, 
			     "Proposal ["+groupPath.toString()+
			     "] is locked at this time by user ["+proposal.getLastEditor()+
			     "]- try again later");
	    }
	    
	    // Check the Group is contained in Proposal.
	    String groupName = groupPath.getGroupByName();
	    Group  group     = proposal.findGroup(groupName);

	    if (group == null) {
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Proposal ["+groupPath.toString()+
			     "] does not contain Group ["+groupName);
	    }
	    
	    // Ok to remove Group.
	    proposal.deleteGroup(group);

	    if (group instanceof FixedGroup) {
		FixedGroup fg = (FixedGroup)group;
		Scheduling.removeFixedGroup(fg);
		logger.log(1, "REMOVE_GROUPImpl", id, "exec",
			   "Removing fixed group: "+fg.getName()+
			   " at "+sdf.format(new Date(fg.getFixedTime())));
	    }

	    // Save the bally Proposal now.
	    try {
		OSS_Utilities.storeProposal(proposal);
	    } catch (IOException ix) {
		logger.log(1, "REMOVE_GROUPImpl", id, "exec",
			   "Proposal ["+groupPath.toString()+
			   "] Unable to Store: "+ix);
		return error(TRANSACTION.DATABASE_UPDATE_ERROR, 
			     "Proposal ["+groupPath.toString()+
			     "] Unable to Store",
			     ix);
	    }
	    
	    REMOVE_GROUP_DONE done = new REMOVE_GROUP_DONE(id);
	    done.setSuccessful(true);
	    return done;
	    
	} finally { 
	    t.commit();
	}
    }
    
}


